import React, { Component, PropTypes } from 'react';
import { connect } from 'dva';
import { WingBlank, WhiteSpace, List, Modal, Button } from 'antd-mobile';
import { routerRedux } from 'dva/router';
import { createForm } from 'rc-form';
import styles from './styles.less';

const RequestListPage = ({ dispatch, Requests, location, ...rest }) => {
	const addScale = (id, status) => {
		Modal.alert('提示', '确定添加该医生为好友？', [
	      { text: '取消', onPress() { } },
	      { text: '确定', onPress: () => {
	        dispatch({
	          type: 'relation/accept',
	          payload: {
	          	id: id,
	          }
	        });
	        dispatch( 
	          routerRedux.replace({
	              pathname: `/doctor`,
	            }),
	        )
	      } }
	    ]);
	}
	const saoScale = () => dispatch({
	    type: 'wechat/scanQRCode',
	    payload: {
	       onSuccess(result) {
            try {
              const doctorId = result.split(':')[1];
              if (doctorId) {
                return dispatch({
                  type: 'relation/send',
                  payload: {
                    doctorId: doctorId,
                  },
                });
              }
            } catch (e) {
              Toast.fail('请扫医生二维码');
            }
          },
    },
  });
	const merScale = () => {
		 dispatch(
		 	routerRedux.push({
		 		pathname: `/settings/qrcode`,
		 	}),
		 );
	}
	return (
		<WingBlank size="md">
	      <WhiteSpace />
	      <List className={styles.doctorList}>
	        <List.Item
	          thumb={require('../../assets/icons/icon-qrcode.svg')}
	          onClick={merScale}>
	          我的二维码
	        </List.Item>
	        <List.Item
	          thumb={require('../../assets/icons/icon-scan.svg')}
	          onClick={saoScale}>
	          扫一扫
	        </List.Item>

	      </List>
	       {Requests?(<List className={styles.doctorList}>
	        {Requests.map(requests =>
	          <List.Item
	            // key={requests.id}
	            thumb={require('../../assets/icons/icon-doctor-thumb.svg')}
	            extra={<button onClick={() =>addScale(requests.id, requests.status)} className={styles.DoctorButton}>接受</button>}>
	              <List.Item.Brief>
	                <span>{requests.name}</span>
	              </List.Item.Brief>
	          </List.Item>
	        )}
	        <WhiteSpace />
	      </List>:""):""}
	     
	    </WingBlank>
	)
}

export default connect(({ relation }) => ({ ...relation }))(createForm()(RequestListPage));